/*
 * @Author: chunlai
 * @Date: 2023-12-07 08:51:58
 * @LastEditTime: 2025-09-05 15:35:59
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\translate\risks\rate.js
 * @Description:
 */
export const GetToRateparams = (rootState, getters) => {
  let { applicant } = rootState.insert,
    pageCfg = rootState.page;
  let assured = getters["insert/assuredList"];
  let risks = rootState.risks;
  const mainObj = risks.find((item) => item.ismain);
  let list = risks.filter((item) => item.ismain || item.checked);
  let params = [],
    data = {};
  if (mainObj.dutyCountCode && mainObj.dutyCountCode.indexOf("as") != -1) {
    params = GetAsToRateData(list, assured[0].socialInsuFlag);
    data = {
      sex: assured[0].sex || "1",
      age: assured[0].age || "0",
      birthDate: assured[0].birthDate || "",
      tbSex: applicant.sex || "1",
      tbage: applicant.age || "0",
      tbBirthDate: applicant.birthDate || "",
      params: params,
      occupation: assured[0].occupationClass || "1",
    };
    return data;
  } else {
    params = GetToRateData(list, rootState.mainRiskCode);
  }
  // console.log('GetToRateparams---', assured.occupationClass);
  let insureds = [];
  const len = assured.length;
  for (let i = 0; i < len; i++) {
    const item = assured[i];
    let bbrItem = {
      sex: item.sex || "1",
      age: item.age || "0",
      name: item.name || "",
      idNum: item.idNum || "",
      birthDate: item.birthDate || "",
      relationToAppnt: item.relationToAppnt || "",
      socialInsuFlag: item.socialInsuFlag,
      occupationClass: item.occupationClass || "1", // 职业等级
      risks: params,
    };
    insureds.push(bbrItem);
  }
  data = {
    appnt: {
      sex: applicant.sex,
      age: applicant.age,
      name: applicant.name,
      idNum: applicant.idNum,
    },
    insureds: insureds,
  };
  return data;
};

export const GetToRateData = (list, mainCode) => {
  let data = list.map((item) => {
    let resData = {
      mainRiskCode: mainCode, //险种编码
      riskCode: item.varietyCode, //险种编码
      riskName: item.productName,
      amnt: item.amnt, //保额
      prem: item.prem, //保费
      mult: item.mult || 1, //份数
      baseMoney: item.baseMoney, // 计算基数
      insureYear: item.insuYear, //保险期间
      insureYearType: item.insuYearType,
      insureDuration: item.payendyear == "趸交" ? "1000" : item.payendyear, //缴费年期
      insureDurationType: item.payendyearType,
    };
    if (item.productCode) {
      resData.productCode = item.productCode;
    }
    if (item.planCode) {
      resData.planCode = item.planCode;
    }
    // “装修保宝”(华安)P091615---待使用测试
    if (item.apiKeyFields) {
      resData.targetApiKeys = item.apiKeyFields;
    }
    /* ==== 多选责任 ==== */
    // 1.组合式
    if (item.dutyPlanList && item.dutyPlanList.length > 0) {
      let arr2 = item.dutyPlanList
          .filter((dut) => dut.isChecked)
          .map((j) => j.dutyCode),
        duty2Code = "";
      const trans2Key = (item.baseDutyCode || "1") + arr2.join("");
      if (item.jibenTrans) {
        for (let key in item.jibenTrans) {
          if (key == trans2Key) {
            duty2Code = item.jibenTrans[key];
          }
        }
      }
      resData.planCode = duty2Code;
    }

    return resData;
  });

  return data;
};

export const GetAsToRateData = (list, socialInsuFlag = "Y") => {
  // console.log("GetAsToRateData---");
  let codeArr = [];
  const mainObj = list.find((item) => item.ismain);
  //1.主计划
  if (mainObj.planCode) {
    codeArr = [mainObj.planCode];
  }
  //1-2
  if (mainObj.zympeCode) {
    const arr3 = mainObj.zympeCode.split("-");
    codeArr = [...codeArr, ...arr3];
  }
  //2.可选责任
  let dutys = [];
  if (mainObj.dutyPlanList) {
    let arr1 = mainObj.dutyPlanList
      .filter((dut) => dut.isChecked)
      .map((j) => j.dutyCode);
    if (arr1 && arr1.length > 0) {
      dutys = arr1.join("-").split("-");
      codeArr = [...codeArr, ...dutys];
    }
  }
  if (mainObj.dutyCountCode === "as-2") {
    let arr5 = [];
    const apiKeys = mainObj.apiKeyFields;
    Object.keys(apiKeys).map((key) => {
      if (
        apiKeys[key] &&
        apiKeys[key] != "no" &&
        apiKeys[key].indexOf("btn") == -1
      ) {
        // console.log(apiKeys[key]);
        arr5.push(apiKeys[key]);
      }
    });

    if (arr5 && arr5.length > 0) {
      dutys = arr5.join("-").split("-");
      codeArr = [...codeArr, ...dutys];
    }
  }
  //3.必选责任
  if (mainObj.mustDutyCodes) {
    const arr4 = mainObj.mustDutyCodes.split("-");
    codeArr = [...codeArr, ...arr4];
  }
  let resData = [];
  codeArr.forEach((ele) => {
    const obj3 = {
      varietyCode: ele,
      payendyear: "1",
      insuYear: "1",
      insuYearType: "Y",
      socialSecFlag: socialInsuFlag,
      planCode: mainObj.planPreCode,
    };
    resData.push(obj3);
  });
  // console.log("---", resData);
  return JSON.stringify(resData);
};
